#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaIconButton.h"
#include "ElaIcon.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QCheckBox>
#include <QPixmap>
#include <QDebug>

class ElaIconButtonDemo : public ElaWidget
{
    Q_OBJECT

  public:
    ElaIconButtonDemo(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaIconButton 功能演示");
        resize(800, 700);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        setupUI();
        setupConnections();
    }

  private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 基础图标按钮展示
        mainLayout->addWidget(createBasicIconButtonGroup());

        // 2. 字体图标按钮展示
        mainLayout->addWidget(createAwesomeIconButtonGroup());

        // 3. 圆角图标按钮展示
        mainLayout->addWidget(createBorderRadiusGroup());

        // 4. 选中状态图标按钮展示
        mainLayout->addWidget(createSelectedStateGroup());

        // 5. 自定义颜色图标按钮展示
        mainLayout->addWidget(createCustomColorGroup());

        // 6. 透明度控制演示
        mainLayout->addWidget(createOpacityGroup());

        mainLayout->addStretch();
    }

    // 创建基础图标按钮组
    QGroupBox *createBasicIconButtonGroup()
    {
        QGroupBox *group = new QGroupBox("基础图标按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同尺寸和类型的图标按钮", this);
        layout->addWidget(infoLabel);

        QHBoxLayout *row1 = new QHBoxLayout();
        QHBoxLayout *row2 = new QHBoxLayout();

        // 小尺寸图标按钮
        ElaIconButton *smallBtn = new ElaIconButton(ElaIconType::House, 16, 32, 32, this);
        smallBtn->setToolTip("小图标按钮 (32x32)");

        // 中尺寸图标按钮
        ElaIconButton *mediumBtn = new ElaIconButton(ElaIconType::GearComplex, 20, 48, 48, this);
        mediumBtn->setToolTip("中图标按钮 (48x48)");

        // 大尺寸图标按钮
        ElaIconButton *largeBtn = new ElaIconButton(ElaIconType::Star, 24, 64, 64, this);
        largeBtn->setToolTip("大图标按钮 (64x64)");

        // 禁用状态图标按钮
        ElaIconButton *disabledBtn = new ElaIconButton(ElaIconType::Heart, 20, 48, 48, this);
        disabledBtn->setToolTip("禁用状态图标按钮");
        disabledBtn->setEnabled(false);

        row1->addWidget(smallBtn);
        row1->addWidget(mediumBtn);
        row1->addWidget(largeBtn);
        row1->addWidget(disabledBtn);

        // 创建图片图标按钮
        // 注意：这里需要实际图片路径，使用示例图标代替
        QPixmap pixmap(48, 48);
        pixmap.fill(Qt::blue);  // 模拟图片
        ElaIconButton *pixmapBtn = new ElaIconButton(pixmap, this);
        pixmapBtn->setFixedSize(48, 48);
        pixmapBtn->setToolTip("图片图标按钮");

        row2->addWidget(pixmapBtn);
        row2->addStretch();

        layout->addLayout(row1);
        layout->addLayout(row2);

        m_basicButtons << smallBtn << mediumBtn << largeBtn << disabledBtn << pixmapBtn;

        return group;
    }

    // 创建字体图标按钮组
    QGroupBox *createAwesomeIconButtonGroup()
    {
        QGroupBox *group = new QGroupBox("Ela字体图标按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同的Ela字体图标", this);
        layout->addWidget(infoLabel);

        QHBoxLayout *row1 = new QHBoxLayout();
        QHBoxLayout *row2 = new QHBoxLayout();

        // 常用功能图标
        ElaIconButton *homeBtn = new ElaIconButton(ElaIconType::House, 20, 40, 40, this);
        homeBtn->setToolTip("首页");

        ElaIconButton *searchBtn = new ElaIconButton(ElaIconType::MagnifyingGlass, 20, 40, 40, this);
        searchBtn->setToolTip("搜索");

        ElaIconButton *settingsBtn = new ElaIconButton(ElaIconType::GearComplex, 20, 40, 40, this);
        settingsBtn->setToolTip("设置");

        ElaIconButton *userBtn = new ElaIconButton(ElaIconType::User, 20, 40, 40, this);
        userBtn->setToolTip("用户");

        // 媒体控制图标
        ElaIconButton *playBtn = new ElaIconButton(ElaIconType::Play, 20, 40, 40, this);
        playBtn->setToolTip("播放");

        ElaIconButton *pauseBtn = new ElaIconButton(ElaIconType::Pause, 20, 40, 40, this);
        pauseBtn->setToolTip("暂停");

        ElaIconButton *stopBtn = new ElaIconButton(ElaIconType::Stop, 20, 40, 40, this);
        stopBtn->setToolTip("停止");

        ElaIconButton *volumeBtn = new ElaIconButton(ElaIconType::VolumeHigh, 20, 40, 40, this);
        volumeBtn->setToolTip("音量");

        row1->addWidget(homeBtn);
        row1->addWidget(searchBtn);
        row1->addWidget(settingsBtn);
        row1->addWidget(userBtn);

        row2->addWidget(playBtn);
        row2->addWidget(pauseBtn);
        row2->addWidget(stopBtn);
        row2->addWidget(volumeBtn);

        layout->addLayout(row1);
        layout->addLayout(row2);

        m_awesomeButtons << homeBtn << searchBtn << settingsBtn << userBtn << playBtn << pauseBtn << stopBtn << volumeBtn;

        return group;
    }

    // 创建圆角图标按钮组
    QGroupBox *createBorderRadiusGroup()
    {
        QGroupBox *group = new QGroupBox("圆角图标按钮", this);
        QHBoxLayout *layout = new QHBoxLayout(group);

        // 直角按钮
        ElaIconButton *btn0 = new ElaIconButton(ElaIconType::Square, 20, 48, 48, this);
        btn0->setBorderRadius(0);
        btn0->setToolTip("直角 (0)");

        // 小圆角按钮
        ElaIconButton *btn4 = new ElaIconButton(ElaIconType::Circle, 20, 48, 48, this);
        btn4->setBorderRadius(4);
        btn4->setToolTip("小圆角 (4)");

        // 中等圆角按钮
        ElaIconButton *btn8 = new ElaIconButton(ElaIconType::Circle, 20, 48, 48, this);
        btn8->setBorderRadius(8);
        btn8->setToolTip("中等圆角 (8)");

        // 大圆角按钮
        ElaIconButton *btn16 = new ElaIconButton(ElaIconType::Circle, 20, 48, 48, this);
        btn16->setBorderRadius(16);
        btn16->setToolTip("大圆角 (16)");

        // 圆形按钮
        ElaIconButton *btn24 = new ElaIconButton(ElaIconType::Circle, 20, 48, 48, this);
        btn24->setBorderRadius(24);
        btn24->setToolTip("圆形 (24)");

        layout->addWidget(btn0);
        layout->addWidget(btn4);
        layout->addWidget(btn8);
        layout->addWidget(btn16);
        layout->addWidget(btn24);

        m_radiusButtons << btn0 << btn4 << btn8 << btn16 << btn24;

        return group;
    }

    // 创建选中状态图标按钮组
    QGroupBox *createSelectedStateGroup()
    {
        QGroupBox *group = new QGroupBox("选中状态图标按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("选中状态的图标按钮会保持高亮背景", this);
        layout->addWidget(infoLabel);

        QHBoxLayout *buttonLayout = new QHBoxLayout();

        // 未选中按钮
        ElaIconButton *unselectedBtn = new ElaIconButton(ElaIconType::Bookmark, 20, 48, 48, this);
        unselectedBtn->setToolTip("未选中状态");
        unselectedBtn->setIsSelected(false);

        // 选中按钮
        ElaIconButton *selectedBtn = new ElaIconButton(ElaIconType::Bookmark, 20, 48, 48, this);
        selectedBtn->setToolTip("选中状态");
        selectedBtn->setIsSelected(true);

        // 动态切换按钮
        ElaIconButton *toggleBtn = new ElaIconButton(ElaIconType::Check, 20, 48, 48, this);
        toggleBtn->setToolTip("点击切换选中状态");
        toggleBtn->setCheckable(true);

        buttonLayout->addWidget(unselectedBtn);
        buttonLayout->addWidget(selectedBtn);
        buttonLayout->addWidget(toggleBtn);

        layout->addLayout(buttonLayout);

        m_selectedButtons << unselectedBtn << selectedBtn << toggleBtn;
        m_toggleButton = toggleBtn;

        return group;
    }

    // 创建自定义颜色图标按钮组
    QGroupBox *createCustomColorGroup()
    {
        QGroupBox *group = new QGroupBox("自定义颜色图标按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("自定义悬停颜色和图标颜色", this);
        layout->addWidget(infoLabel);

        QHBoxLayout *colorLayout = new QHBoxLayout();
        QHBoxLayout *resetLayout = new QHBoxLayout();  // 新增：重置按钮布局

        // 蓝色主题按钮
        ElaIconButton *blueBtn = new ElaIconButton(ElaIconType::House, 20, 48, 48, this);
        blueBtn->setLightHoverColor(QColor(33, 150, 243, 50));
        blueBtn->setDarkHoverColor(QColor(33, 150, 243, 80));
        blueBtn->setLightIconColor(QColor(33, 150, 243));
        blueBtn->setDarkIconColor(QColor(100, 181, 246));
        blueBtn->setLightHoverIconColor(QColor(25, 118, 210));
        blueBtn->setDarkHoverIconColor(QColor(66, 165, 245));
        blueBtn->setToolTip("蓝色主题");

        // 绿色主题按钮
        ElaIconButton *greenBtn = new ElaIconButton(ElaIconType::Check, 20, 48, 48, this);
        greenBtn->setLightHoverColor(QColor(76, 175, 80, 50));
        greenBtn->setDarkHoverColor(QColor(76, 175, 80, 80));
        greenBtn->setLightIconColor(QColor(76, 175, 80));
        greenBtn->setDarkIconColor(QColor(129, 199, 132));
        greenBtn->setLightHoverIconColor(QColor(56, 142, 60));
        greenBtn->setDarkHoverIconColor(QColor(102, 187, 106));
        greenBtn->setToolTip("绿色主题");

        // 红色主题按钮
        ElaIconButton *redBtn = new ElaIconButton(ElaIconType::Heart, 20, 48, 48, this);
        redBtn->setLightHoverColor(QColor(244, 67, 54, 50));
        redBtn->setDarkHoverColor(QColor(244, 67, 54, 80));
        redBtn->setLightIconColor(QColor(244, 67, 54));
        redBtn->setDarkIconColor(QColor(229, 115, 115));
        redBtn->setLightHoverIconColor(QColor(211, 47, 47));
        redBtn->setDarkHoverIconColor(QColor(239, 83, 80));
        redBtn->setToolTip("红色主题");

        // 紫色主题按钮
        ElaIconButton *purpleBtn = new ElaIconButton(ElaIconType::Star, 20, 48, 48, this);
        purpleBtn->setLightHoverColor(QColor(156, 39, 176, 50));
        purpleBtn->setDarkHoverColor(QColor(156, 39, 176, 80));
        purpleBtn->setLightIconColor(QColor(156, 39, 176));
        purpleBtn->setDarkIconColor(QColor(186, 104, 200));
        purpleBtn->setLightHoverIconColor(QColor(123, 31, 162));
        purpleBtn->setDarkHoverIconColor(QColor(171, 71, 188));
        purpleBtn->setToolTip("紫色主题");

        colorLayout->addWidget(blueBtn);
        colorLayout->addWidget(greenBtn);
        colorLayout->addWidget(redBtn);
        colorLayout->addWidget(purpleBtn);

        // 新增：重置按钮
        QPushButton *resetButton = new QPushButton("重置所有颜色为默认", this);
        resetButton->setFixedSize(150, 30);
        resetLayout->addWidget(resetButton);
        resetLayout->addStretch();

        layout->addLayout(colorLayout);
        layout->addLayout(resetLayout);  // 添加重置按钮布局

        m_colorButtons << blueBtn << greenBtn << redBtn << purpleBtn;

        // 连接重置按钮信号
        connect(resetButton, &QPushButton::clicked, this, [this]() {
            for (ElaIconButton *btn : m_colorButtons)
            {
                btn->resetToDefaultColors();
                btn->setToolTip("默认颜色");
            }
            qDebug() << "所有按钮颜色已重置为默认值";
        });

        return group;
    }

    // 创建透明度控制演示组
    QGroupBox *createOpacityGroup()
    {
        QGroupBox *group = new QGroupBox("透明度控制演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("演示不同透明度的图标按钮", this);
        layout->addWidget(infoLabel);

        QHBoxLayout *opacityLayout = new QHBoxLayout();

        // 全不透明按钮
        ElaIconButton *opaqueBtn = new ElaIconButton(ElaIconType::Circle, 20, 48, 48, this);
        opaqueBtn->setOpacity(1.0);
        opaqueBtn->setToolTip("不透明度: 1.0");

        // 半透明按钮
        ElaIconButton *semiBtn = new ElaIconButton(ElaIconType::Circle, 20, 48, 48, this);
        semiBtn->setOpacity(0.7);
        semiBtn->setToolTip("不透明度: 0.7");

        // 较透明按钮
        ElaIconButton *transparentBtn = new ElaIconButton(ElaIconType::Circle, 20, 48, 48, this);
        transparentBtn->setOpacity(0.4);
        transparentBtn->setToolTip("不透明度: 0.4");

        // 几乎透明按钮
        ElaIconButton *veryTransparentBtn = new ElaIconButton(ElaIconType::Circle, 20, 48, 48, this);
        veryTransparentBtn->setOpacity(0.2);
        veryTransparentBtn->setToolTip("不透明度: 0.2");

        opacityLayout->addWidget(opaqueBtn);
        opacityLayout->addWidget(semiBtn);
        opacityLayout->addWidget(transparentBtn);
        opacityLayout->addWidget(veryTransparentBtn);

        layout->addLayout(opacityLayout);

        m_opacityButtons << opaqueBtn << semiBtn << transparentBtn << veryTransparentBtn;

        return group;
    }

    void setupConnections()
    {
        // 切换按钮的选中状态
        connect(m_toggleButton, &ElaIconButton::toggled, this, [this](bool checked) {
            m_toggleButton->setIsSelected(checked);
            if (checked)
            {
                m_toggleButton->setToolTip("已选中状态");
            }
            else
            {
                m_toggleButton->setToolTip("未选中状态");
            }
        });

        // 所有按钮的点击事件
        auto allButtons = m_basicButtons + m_awesomeButtons + m_radiusButtons + m_selectedButtons + m_colorButtons + m_opacityButtons;

        for (ElaIconButton *btn : allButtons)
        {
            connect(btn, &ElaIconButton::clicked, this, [btn]() { qDebug() << "图标按钮被点击:" << btn->toolTip(); });
        }

        // 动态图标切换演示
        connect(m_awesomeButtons[0], &ElaIconButton::clicked, this, [this]() {
            static bool toggle = false;
            if (toggle)
            {
                m_awesomeButtons[0]->setAwesome(ElaIconType::House);
                m_awesomeButtons[0]->setToolTip("首页");
            }
            else
            {
                m_awesomeButtons[0]->setAwesome(ElaIconType::HouseChimneyHeart);
                m_awesomeButtons[0]->setToolTip("首页(填充)");
            }
            toggle = !toggle;
        });
    }

  private:
    QList<ElaIconButton *> m_basicButtons;
    QList<ElaIconButton *> m_awesomeButtons;
    QList<ElaIconButton *> m_radiusButtons;
    QList<ElaIconButton *> m_selectedButtons;
    QList<ElaIconButton *> m_colorButtons;
    QList<ElaIconButton *> m_opacityButtons;
    ElaIconButton *m_toggleButton = nullptr;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaIconButtonDemo demo;
    demo.moveToCenter();
    demo.show();

    return app.exec();
}

#include "ElaIconButtonTest.moc"